#include "stm32f10x.h"                  // Device header
#include "pwm_AT8236.h"

/**
 * @brief PWM Initial
 * @param arr value,
 * @param psc value,
 * @retval NULL
*/
void TIM3_PWM_Init(uint16_t arr, uint16_t psc)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			// 复用推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;	//PB0, PB1
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		// IO口速率
	GPIO_Init(GPIOB, &GPIO_InitStructure);					// GPIO初始化

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			// 复用推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;	// PA6, PA7
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		// IO口速率
	GPIO_Init(GPIOA, &GPIO_InitStructure);					// GPIO初始化
	
	TIM_InternalClockConfig(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);				// 初始化时基单元默认值
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;	// 向上计数
	TIM_TimeBaseInitStructure.TIM_Period = arr;			//ARR 设置下一个更新活动的自动冲撞载寄存器
	TIM_TimeBaseInitStructure.TIM_Prescaler = psc;		//PSC 预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;	// 时钟分割
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);		// 时基单元初始化
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;				// PWM脉冲宽度调制1
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;		// 设置TIM输出极性为高
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	// 比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;		//CCR 设置待装入捕获比较寄存器的脉冲值
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);

	TIM_CtrlPWMOutputs(TIM3, ENABLE);		// 主输出使能

	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);	// 使能预装载寄存器
	
	TIM_ARRPreloadConfig(TIM3, ENABLE);				// 使能自动装载允许位
	TIM_Cmd(TIM3, ENABLE);							// 启动定时器3
}

/**
 * @brief Set TIM3 PWM Value
 * @param PWM value
 * @retval NULL
*/
void Set_PWM(int16_t PWM)
{
	PWMA_IN1 = 7200;
	PWMA_IN2 = 7200 - PWM;
}


// PWM frequency: 	Freq = CK_PSC/(PSC + 1) / (ARR + 1)
// PWM duty cycle: 	Duty = CCR / (ARR + 1)
// PWM resolution: 	Reso = 1 / (ARR + 1)


// void PWM_SetCompare1(uint16_t Compare)
// {
// 	TIM_SetCompare1(TIM2, Compare);
// }

// void PWM_SetPrescaler(uint16_t Prescaler)
// {
// 	TIM_PrescalerConfig(TIM2, Prescaler, TIM_PSCReloadMode_Immediate);
// }
